// pages/overhaulDetail/overhaulDetail.js
const app = getApp();
import CommonService from '../../utils/service/commonService.js';
import Toast from '../../miniprogram_npm/@vant/weapp/toast/toast';
const util = require('../../utils/util');
import Base64 from '../../utils/Base64.js';

Page({

  /**
   * 页面的初始数据
   */
  data: {
    //传参来的设备信息
    equipmentInfo:null,
    //传参来的id
    id:null,    
    //修改信息回显
    editInfo:{},    
    //检修详情
    detailMap:new Map(),
    //售后人员
    staffMap: new Map()     
  },  

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if(options.id){
      this.setData({ 
        id: parseInt(options.id)       
      })

      //查询检修详情(字典)
      this.overhaulDetail();           
    }  
  },  

  //获取检修信息
  getInfo(){
    let param = {
      isAuth: false,
      className: 'afterSaleOverhaul',
      data: {
        id:this.data.id,
        selectFields: ['id','departId','depart','overhaulPersonnel','service','overhaulDate','partMoney','serviceMoney','logisticsMoney','pic','equipmentState','remark','result','detail','equipmentObj'],
        nbl: Base64.encode(util.timeFormatDay(new Date()))
      }
    }

    CommonService.goFindList(param).then(res => {
      if(res.data.data && res.data.data.result && res.data.data.result.length) {
        let result=res.data.data.result[0];

        //处理时间
        if(result.overhaulDate){
          result.overhaulDate=util.timeFormat(result.overhaulDate); 
        }

        //检修人员和电话
        if(result.overhaulPersonnel){            
          let maps=this.data.staffMap;
          if(maps.get(result.overhaulPersonnel)){
            result.self_staffName=maps.get(result.overhaulPersonnel).name;
            result.self_staffPhone=maps.get(result.overhaulPersonnel).phone;
          }            
        }

        this.setData({
          editInfo: result
        })

        //修改信息回显
        this.getInputValue();
      }
    }).catch((error) => {
    })  
  },

  //修改信息回显
  getInputValue(){
    Toast.clear();

    let info=this.data.editInfo;

    console.log('修改信息回显');
    console.log(info);
    
    //处理明细
    let detail=info.detail;
    let detailArr=[];
    if(detail.length){
      let detailMap=this.data.detailMap;      
      detail.forEach(item => {              
        detailArr.push(detailMap.get(item));                            
      });      
    }

    this.setData({
      imgUrls:JSON.parse(info.pic),
      detailArr:detailArr.join(', ')
    })
  },
  
  //查询检修详情(字典)
  overhaulDetail(){
    Toast.loading({
      duration: 0,
      forbidClick: true,
      message: '加载中...'
    });

    let param1 = {
      isAuth: false,
      className: 'dictionary',
      data: {
        searchAll: true,
        key: 'overhaul',
        selectFields: ['id']
      }
    }

    CommonService.goFindList(param1).then(res=>{
      if(res.data.data && res.data.data.result && res.data.data.result.length){
        let dictionaryId = res.data.data.result[0].id;

        let param2 = {
          isAuth: false,
          className: 'dictionaryDetail',
          data: {
            searchAll: true,
            dictionaryId: dictionaryId,
            selectFields: ['id','fieldValue']
          }
        }
        CommonService.goFindList(param2).then(res2=>{
          if(res2.data.data && res2.data.data.result && res2.data.data.result.length){
            let dictionaryDetails=res2.data.data.result;        
            let detailMap = new Map();
            dictionaryDetails.forEach(detail => {          
              detailMap.set(detail.id,detail.fieldValue);
            })

            this.setData({
              detailMap: detailMap         
            })

            //查询售后人员
            this.getStaffList();
          }
        }).catch((error) => {
          Toast.clear();
        })
      }
    }).catch((error) => {
      Toast.clear();
    })
  },

  //预览图片
  onPreviewImage(e) {
    let detail = e.target.dataset.detail;    
    let src = e.target.dataset.src;    

    let newArr=[];
    for(let i=0;i<detail.length;i++){
      newArr.push(detail[i].url);
    }    

    wx.previewImage({
      current: src,
      urls: newArr
    })
  },

  //查询售后人员
  getStaffList(){
    console.log('1');
    let param = {
      isAuth: false,
      className: 'servicePersonal',
      data: {
        searchAll: true,
        selectFields: ['id','serviceName','telephone'],
        nbl:Base64.encode(util.timeFormatDay(new Date()))
      }
    }

    //按平台查询记录
    let appId=app.globalData.myAppId;
    if(appId!=3){
      if(app.globalData.afterSaleSelf){
        //是否独立售后
        param.data.appId=app.globalData.myAppId;
      }else{
        param.data.appId=[2,3,null];
      }
    }else{
      param.data.appId=[2,3,null];
    }  

    CommonService.goFindList(param).then(res=>{
      if(res.data.data && res.data.data.result && res.data.data.result.length){
        let result=res.data.data.result;

        let map=new Map();
        result.forEach(item => {
          map.set(
            item.id,
            {
              name: item.serviceName,
              phone: item.telephone
            }
          );
        });

        console.log('售后人员');
        console.log(map);

        this.setData({
          staffMap: map
        }) 
        
        //获取检修信息
        this.getInfo();
      }
    }).catch((error) => {
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  }
})